package com.masterclient.app.modules.shop;

import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.bean.shop.DealerProductManagerData;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.commonbean.product.ShareProductDataBean;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.adapter.DealerSoldOutProductAdapter;
import com.masterclient.app.modules.shop.contract.DealerProductManagerContract;
import com.masterclient.app.modules.shop.contract.DealerProductManagerPresenter;
import butterknife.BindView;

/**
 * @auther hxm
 * @date 2020/9/11.
 */
public class DealerSoldOutProductFragment extends BaseFragment<DealerProductManagerPresenter>
        implements DealerProductManagerContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    private int page,status,productType;
    private View headView, emptyView;
    private TextView headContentView;
    private DealerSoldOutProductAdapter dealerSoldOutProductAdapter;

    public static DealerSoldOutProductFragment newInstance(int status,int productType) {
        DealerSoldOutProductFragment fragment = new DealerSoldOutProductFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("status", status);
        bundle.putInt("productType", productType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_recycler_refresh, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        page = 1;
        status = getArguments().getInt("status");
        productType = getArguments().getInt("productType");
        emptyView = View.inflate(mContext,R.layout.empty_recycler,null);
        ((TextView)emptyView.findViewById(R.id.empty_title)).setTextColor(Color.parseColor("#C2C5CC"));

        recyclerView.setPadding(ScreenUtils.dp2px(16),ScreenUtils.dp2px(10),ScreenUtils.dp2px(16),0);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = ScreenUtils.dp2px(10);
            }
        });

        dealerSoldOutProductAdapter = new DealerSoldOutProductAdapter();
        headView = View.inflate(getActivity(), R.layout.head_dealer_product_manager, null);
        headContentView = headView.findViewById(R.id.head_dealer_manager_text);
        View headCloseView = headView.findViewById(R.id.head_dealer_manager_close);
        View headNewsView = headView.findViewById(R.id.head_dealer_manager_icon);
        headNewsView.setVisibility(View.VISIBLE);
        headCloseView.setVisibility(View.GONE);
        dealerSoldOutProductAdapter.addHeaderView(headView);

        dealerSoldOutProductAdapter.setEmptyView(emptyView);
        emptyView.setVisibility(View.GONE);
        recyclerView.setAdapter(dealerSoldOutProductAdapter);

        refreshData(1);
        initListener();
    }

    private void initListener() {
        swipeLayout.setOnRefreshListener(() -> refreshData(1));

        dealerSoldOutProductAdapter.setOnLoadMoreListener(() -> refreshData(page),recyclerView);
    }

    public void refreshData(int page){
        if (page == 1){
            recyclerView.stopScroll();
            recyclerView.scrollToPosition(0);
        }
        this.page = page;
        mPresenter.getProductList(String.valueOf(status), page,String.valueOf(productType));
    }

    @Override
    public void showData(DealerProductManagerData dataBean) {
        if (swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        dealerSoldOutProductAdapter.loadMoreComplete();
        if (page == 1){
            //头部视图
            if (!TextUtils.isEmpty(dataBean.getTitle_info())){
                headContentView.setText(dataBean.getTitle_info());
                if (dealerSoldOutProductAdapter.getHeaderLayoutCount()<=0){
                    dealerSoldOutProductAdapter.addHeaderView(headView);
                }
            }else {
                dealerSoldOutProductAdapter.removeAllHeaderView();
            }

            //列表数据
            if (dataBean.getList() != null){
                dealerSoldOutProductAdapter.setNewData(dataBean.getList());
                ((DealerSoldOutProductActivity)getActivity()).setTabName(productType-2, String.valueOf((productType ==2?dataBean.getOnsale_total():dataBean.getOffsale_total())),productType);
            }
            //空视图显示状态
            if (productType == 2){
                ((TextView)emptyView.findViewById(R.id.empty_title)).setText("暂时没有经销商品");
            }else {
                ((TextView)emptyView.findViewById(R.id.empty_title)).setText("暂时没有轻创商品");
            }
            emptyView.setVisibility(dataBean.getList() == null || dataBean.getList().size() == 0 ?View.VISIBLE:View.GONE);
        }else {
            //分页数据加载
            dealerSoldOutProductAdapter.addData(dataBean.getList());
        }

        //分页状态
        if(dataBean.getList() == null || dataBean.getList().size()<1 || dealerSoldOutProductAdapter.getItemCount()>=(productType ==2?dataBean.getOnsale_total():dataBean.getOffsale_total())){
            dealerSoldOutProductAdapter.setEnableLoadMore(false);
        }else {
            ++page;
        }
    }

    @Override
    public void showDataFailed(String msg) {
        ToastUtil.showToast(msg);
        if (swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        dealerSoldOutProductAdapter.loadMoreComplete();
    }

    @Override
    public void showShareData(boolean isSuccess, ShareProductDataBean shareData) {

    }
}
